package com.shundr.route;

import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.shundr.R;
import com.shundr.base.BaseActivity;
import com.shundr.common.AreaGridActivity;
import com.shundr.common.dao.RouteDao;
import com.shundr.common.model.AreaInfo;
import com.shundr.common.util.JsonUtil;
import com.shundr.common.util.ProgressDialogHelper;
import com.shundr.common.util.ToastHelper;
import com.shundr.frame.http.LHttpRequest;
import com.shundr.frame.util.StringUtil;
import com.shundr.route.http.RouteRequest;
import com.shundr.route.model.TruckerRouteInfo;

/**
 * 常跑路线
 * RouteListActivity
 * 
 * @author larry 2015-4-22 上午11:28:08 
 * @version 1.0.0
 *
 */
public class RouteAddActivity extends BaseActivity implements OnClickListener{
	
	private LinearLayout mLayoutStart;
	private LinearLayout mLayoutEnd;

	private TextView tvStart;
	private TextView tvEnd;
	private Button btnAdd;
	
	private String startCode;
	private String endCode;
	
	private RouteDao mRouteDao;
	
	private List<TruckerRouteInfo> mList = new ArrayList<TruckerRouteInfo>();
	
	@SuppressLint("HandlerLeak")
	private Handler mHandler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			try {
				switch (msg.what) {
				case RouteRequest.GET_ROUTE_LIST_SUCCESSED:
					List<TruckerRouteInfo> infos = (List<TruckerRouteInfo>)msg.obj;
					if(infos!=null){
						mList.clear();
						mList.addAll(infos);
						mRouteDao.syncData(infos);
					}
					break;
				case RouteRequest.ADD_ROUTE_SUCCESSED:
					new RouteRequest(mContext, mHandler).getRouteList();
					ProgressDialogHelper.dismiss();
					ToastHelper.toastShort(mContext, "添加线路成功");
					new Timer().schedule(new TimerTask() {
						
						@Override
						public void run() {
							finish();
						}
					}, 500);
					break;
				case LHttpRequest.REQUEST_FAILED:
					ProgressDialogHelper.dismiss();
					ToastHelper.toastShort(mContext, msg.obj.toString());
					break;

				default:
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
			super.handleMessage(msg);
		}
		
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_route);
		mLayoutStart = (LinearLayout) findViewById(R.id.layout_start);
		mLayoutEnd = (LinearLayout) findViewById(R.id.layout_end);
		tvStart = (TextView) findViewById(R.id.tv_start);
		tvEnd = (TextView) findViewById(R.id.tv_end);
		btnAdd = (Button) findViewById(R.id.btn_add);
		mLayoutStart.setOnClickListener(this);
		mLayoutEnd.setOnClickListener(this);
		btnAdd.setOnClickListener(this);
		mRouteDao=new RouteDao();
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.layout_start:
			Intent start = new Intent(mContext, AreaGridActivity.class);
			start.putExtra("title", "始发地");
//			start.putExtra("code", startCode);
			startActivityForResult(start, 500);
			break;
		case R.id.layout_end:
			Intent end = new Intent(mContext, AreaGridActivity.class);
			end.putExtra("title", "目的地");
			startActivityForResult(end, 501);
			break;
		case R.id.btn_add:
			if (startCode == null || StringUtil.isNull(startCode)) {
				ToastHelper.toastShort(mContext, "请选择具体始发地");
			} else if (endCode == null || StringUtil.isNull(endCode)) {
				ToastHelper.toastShort(mContext, "请选择具体目的地");
			} else {
				try {
					boolean p=true;
					for(int i=0;i<mList.size();i++){
						String dcode=mList.get(i).cargoDstCode+"";
						String scode=mList.get(i).cargoSrcCode+"";
						if((endCode.equals(dcode))&&(startCode.equals(scode))){
							ToastHelper.toastShort(mContext, "您已经添加过此路线");
							p=false;
						}else{
							p=true;
						}						
					}
					if(p){
						ProgressDialogHelper.show(mContext, "正在提交数据,请稍后...");
						new RouteRequest(mContext, mHandler).addRoute(startCode, endCode);
					}
				} catch (Exception e) {
					e.printStackTrace();
				}					
			}
			break;

		default:
			break;
		}
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		switch (requestCode) {
		case 500:
			if(data != null){
				try {
					String json = data.getStringExtra("json");
					List<AreaInfo> areas = JsonUtil.getList(json, AreaInfo.class);
					if(areas.size() > 0){
						tvStart.setText(areas.get(0).getName());
						startCode = areas.get(0).getCode();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			break;
		case 501:
			if(data != null){
				try {
					String json = data.getStringExtra("json");
					List<AreaInfo> areas = JsonUtil.getList(json, AreaInfo.class);
					if(areas.size() > 0){
						tvEnd.setText(areas.get(0).getName());
						endCode = areas.get(0).getCode();
					}
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			break;

		default:
			break;
		}
	}
	
}
